#include "Level.hpp"

const unsigned int Level::MAX = 20;

const unsigned int LEVEL_TABLE[Level::MAX + 1] = {
    // Experience required.
    0, // Level 0
    10,
    30,
    60,
    100,
    150,
    210,
    280,
    360,
    450,
    550, // Level 10
    660,
    780,
    910,
    1050,
    1200,
    1360,
    1530,
    1710,
    1900,
    2100 // Level 20
};
const unsigned int Level::MAX_EXP = 2100; // Should be last.

Level::Level()
{
    m_exp = 0;
    m_lvl = 0;
}



Level::~Level()
{

}



unsigned int Level::GetExperience() const
{
    return m_exp;
}



unsigned int Level::GetExperienceToLevel() const
{
    return LEVEL_TABLE[m_lvl + 1] - m_exp;
}



unsigned int Level::GetLevel() const
{
    return m_lvl;
}



unsigned int Level::AddExperience(unsigned int exp)
{
    unsigned int lvlups = 0;

    // Check upper bound.
    if (m_exp + exp >= MAX_EXP)
    {
        m_exp = MAX_EXP;
        return lvlups;
    }
    
    // Check for multiple level ups.
    while (exp >= GetExperienceToLevel())
    {
        exp -= GetExperienceToLevel();
        LevelUp();
        lvlups++;
        
        // Check if hit max.
        if (m_lvl == MAX)
            return lvlups;
    }
    
    m_exp += exp;
    
    return lvlups;
}



void Level::LevelUp()
{
    if (m_lvl == MAX)
        return;

    m_lvl++;
    
    m_exp = LEVEL_TABLE[m_lvl];
}
